<?php

namespace App\Abstracts\Controllers;

use Carbon\Carbon;
use Hilaoyu\LaravelExtend\ControllerBaseResource;
use Hilaoyu\LaravelExtend\Services\Captcha\CaptchaService;
use Hilaoyu\Utils\UtilHtml;

use Hilaoyu\Utils\UtilSupport;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;


abstract class AbstractControllerAuthUsers extends ControllerBaseResource
{

    protected function alterModelsQuery($query,Request $request)
    {
        $query = parent::alterModelsQuery($query,$request);
        $search = [
            'keyword' => $request->input('keyword',''),
            'status' => $request->input('status',''),
            'role' => $request->input('role',''),
        ];



        if($search['status'] != ''){
            if('1' == $search['status']){
                $query = $query->where(function ($q){
                    return $q->orWhere('lock_to','<=',Carbon::now()->toDateTimeString())
                        ->orWhereNull('lock_to')
                        ;
                });
            }else{
                $query = $query->where('lock_to','>',Carbon::now()->toDateTimeString());
            }

        }

        if($search['role'] != ''){
            $query = $query->whereHas('roles',function ($query) use($search){
                $query->where('id', '=', $search['role']);
            });
        }

        /*$role_options = [];
        if(static::classNameModelRoles()){
            $roles = UtilSupport::makeClass(static::classNameModelRoles())->select(function ($query){return $query->where('status','=',1);});
            $roles_arr = $roles->toArray();
            $roles_arr = Arr::prepend($roles_arr,[
                'id'=>'',
                'name'=>'用户组',
            ]);

            $role_options = UtilHtml::formatHtmlOptions($roles_arr,'id','name','',$search['role']);
        }*/

        if(!empty($search['keyword'])){
            $query = $query->where(function ($query) use ($search){
                $query->orWhere('account','like','%'. $search['keyword'] .'%')
                    //->orWhere('name','like','%'. $search['keyword'] .'%')
                    ->orWhere('summary','like','%'. $search['keyword'] .'%')
                    ->orWhere('email','like','%'. $search['keyword'] .'%')
                    ->orWhere('mobile','like','%'. $search['keyword'] .'%');
            });
        }

        comResponse()->with( $search,'search')
            //->with( $role_options,'role_options')
            ->with( UtilHtml::statusOptions($search['status'],'是否启用'),'status_options');
        return $query;
    }

    protected function alterModelsLoaded($models, Request $request)
    {
        $models = parent::alterModelsLoaded($models, $request);

        UtilSupport::tryQuiet(function () use (&$models){
            $models->load('roles');
        });

        return $models;
    }


    protected function alterModelLoaded($model, Request $request)
    {
        $model = parent::alterModelLoaded($model, $request);

        if($model){
            UtilSupport::tryQuiet(function () use (&$model){
                $model->load('roles');
            });
        }

        /*$role_options = [];
        if(static::classNameModelRoles()){
            $roles = UtilSupport::makeClass(static::classNameModelRoles())->select(function ($query){return $query->where('status','=',1);});
            $role_selected = [];
            if($model && !empty($model->roles)){
                $role_selected = $model->roles->pluck('id')->toArray();
            }
            $role_options = UtilHtml::formatHtmlOptions($roles->toArray(),'id','name','roles[]',$role_selected);
        }*/


        comResponse()
            //->with( $role_options,'role_options')
            ->with( UtilHtml::sexOptions(($model->sex ?? '保密')),'sex_options')
            ->with( UtilHtml::statusOptions(($model->is_super_administrator ?? '0')),'is_super_options');
        return $model;
    }


    protected function alterModelSaveData(Request $request,$id='')
    {
        $data = [
            //'account' => $request->input('account',''),
            'name' => $request->input('name',''),
            'summary' => $request->input('summary',''),
            'sex' => $request->input('sex',''),
            'email' => $request->input('email',''),
            'mobile' => $request->input('mobile',''),
            'picture' => $request->input('picture',''),
            'lock_to' => $request->input('lock_to',null),
            'is_super_administrator' => $request->input('is_super_administrator',0),
            'roles' => $request->input('roles',[]),
        ];

        if(empty($id)){
            $data['account'] = $request->input('account','');
        }

        if($pass = $request->input('password',null)){
            $data['password'] = $pass;
        }

        return $data;
    }



}
